package com.lym.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lym.reggie.common.R;
import com.lym.reggie.entity.Orders;
import com.lym.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    //分页查询
    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, String beginTime, String endTime){
        Page<Orders> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number!=null,Orders::getNumber,number)
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime)
                .lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);

        ordersService.page(pageInfo);

        return R.success(pageInfo);
    }

    //支付
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        //remark payMethod addressBookid
        ordersService.submit(orders);
        return R.success("下单成功！");
    }

    //查看订单
    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        //查看该用户的所有订单
        Page pageInfo = ordersService.getUserPage(page,pageSize);

        return R.success(pageInfo);
    }

    //再来一单
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        Long orderId = orders.getId();
        //清除购物车数据、添加此订单中的数据
        ordersService.again(orderId);

        return R.success("成功");
    }
}
